package com.seeyon.apps.ext.cop.button.login;

import com.aspose.imaging.internal.bI.S;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.authenticate.domain.User;
import com.seeyon.ctp.common.config.manager.HttpSecurityFilterManager;
import org.opensaml.ws.transport.http.HttpServletResponseAdapter;
import snail.common.web.HTTPRequestHelper;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/*")
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;


        User user = AppContext.getCurrentUser();

        System.out.println("用户 = " +user);


        String  a = request.getQueryString();

        // /seeyon
        // /
        String uri = request.getRequestURI();

        System.out.println("后缀" + uri);

        if(null == user){

            if(uri.equalsIgnoreCase("/")
                    || uri.equalsIgnoreCase("/seeyon")
                    || uri.equalsIgnoreCase("/seeyon/")
                    || uri.equalsIgnoreCase("/seeyon/index.jsp")
                    || uri.startsWith("/seeyon/mian.do")){

                ((HttpServletResponse) servletResponse).sendRedirect("http://10.37.129.2:8081/login?redirect_url=" + HTTPRequestHelper.encode("http://127.0.0.1/seeyon/login/sso"));

                return;
            }
        }

        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}
